package com.gtq.uhome.controller;

import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.UserLoginService;
import com.gtq.uhome.util.JWTUtil;
import com.gtq.uhome.vo.UserOrderCheckVo;
import com.gtq.uhome.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户前端登录controller
 */
@RestController
@RequestMapping(value = "/userLogin")
@CrossOrigin
@Api(tags = "用户前端登录模块接口说明文档 咨询李龙")
public class UserLogincontroller {
    @Resource
    private UserLoginService userLoginService;

    /**
     * 用户注册界面信息展示
     * @param map 站点名字
     * @return 返回用户协议和站点客服电话
     */
    @PostMapping("/registerInfo")
    @ApiOperation(value = "用户注册界面信息用户协议和客服电话", notes = "用户登录界面信息展示，需要字段siteName:站点名称，返回用户协议和站点客服电话。没有站点名，则返回总站点客服电话", response = Map.class, httpMethod = "POST")
    public Result registerInfo(@RequestBody Map<String,String> map){
        String siteName = map.get("siteName");
        Map<String, String> registerInfo = userLoginService.registerInfo(siteName);
        if (registerInfo!=null){
            return Result.ok(registerInfo,"查询成功");
        }
        return Result.failed("查询失败");
    }
    /**
     * 用户注册界面
     * @param map 用户注册信息
     * @return true注册成功 false 注册失败
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册，一个手机号只能注册一个用户", notes = "用户注册，需要一些用户信息，字段如下：userName:用户名，userPhoneNumber:用户电话号码", response = Map.class, httpMethod = "POST")
    public Result register(@RequestBody Map<String,String> map){
        String userName = map.get("userName");
        String userPhoneNumber = map.get("userPhoneNumber");
        System.out.println("收到的用户名："+userName+"收到的用户手机号："+userPhoneNumber);
        Boolean register = userLoginService.register(userName, userPhoneNumber);
        if (register){
            return Result.ok(null,"注册成功");
        }
        return Result.failed("注册失败，你已经注册过");
    }


    /**
     * 用户登录
     *
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录接口", notes = "用户登录接口，用户登录输入手机号发送验证码。需要如下字段phoneNumber:用户手机号码，verificationCode:短信验证码,登录成功返回一个令牌（token）Authorization：令牌值和userId:用户id", response = Map.class, httpMethod = "POST")
    public Result userLogin(@RequestBody Map<String, Object> map) {
        String phoneNumber = (String) map.get("phoneNumber");
        String StrVerificationCode = (String) map.get("verificationCode");
        int verificationCode = Integer.parseInt(StrVerificationCode);
        System.out.println("接收到的信息："+map);
        //System.out.println("收到的手机号：" + phoneNumber + "收到的验证码：" + verificationCode);
        //调用service层用户等方法
        UserVo userVo = userLoginService.userLogin(phoneNumber, verificationCode);
        //返回信息给前端
        if (userVo != null) {
            //使用JWT工具类
            JWTUtil jwtUtil = new JWTUtil();
            String id = String.valueOf(System.currentTimeMillis());
            //subject 使用json
            String subject = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}";
            //创建JWT
            String jwt = jwtUtil.createJWT(id, subject, 48 * 60 * 60, map);
            //生成令牌
            Map<String, Object> authorizationMap = new HashMap();
            authorizationMap.put("userInfo",userVo);
            authorizationMap.put("Authorization", jwt);
            return Result.ok(authorizationMap, "登录成功");
        }
        return Result.failed("登录失败，请检查手机号和验证码");

    }

    /**
     * 发送验证码
     * @return 返回true 成功 false 失败
     */
    @PostMapping("/SendVerificationCode")
    @ApiOperation(value = "验证码接口", notes = "发送验证码给用户,需要用户电话号码字段userPhoneNumber", response = Map.class, httpMethod = "POST")
    public Result SendVerificationCode(@RequestBody Map<String,String> map){
        String userPhoneNumber = map.get("userPhoneNumber");
        //调用发送验证码方法
        /*Integer verificationCode = userLoginService.sendVerificationCode(userPhoneNumber);
        if (verificationCode!=0)
            return Result.ok(null,"发送成功");*/
        return Result.failed("发送失败，稍后再试");
    }

    @PostMapping("/findCheck")
    @ApiOperation(value = "用户审核订单列表",notes = "用户订单为‘待审核‘的列表")
    public Result<List<UserOrderCheckVo>> findUserCheckOrder(@RequestBody Map<String,Integer> userId){
        return Result.ok(userLoginService.findUserCheckOderList(userId.get("userId")),"查询成功");

    }
    /**
     * 用户订单消息
     * @return 返回消息集合
     */
    @PostMapping("/userMsg")
    @ApiOperation(value = "用户消息接口", notes = "用户消息中心，需要传送用户ID", response = Map.class, httpMethod = "POST")
    public Result userMsg(@RequestBody Map<String,String> map){
        String userId = map.get("userId");
        int userID = Integer.parseInt(userId);
        //获取到的用户ID
        System.out.println("收到的用户ID");
        //调用查询消息方法
        List<String> strings = userLoginService.userMsg(userID);
        if (strings!=null){
            return Result.ok(strings,"查询成功");
        }
        return Result.failed("该用户暂无订单消息");
    }



}
